@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM =============================================================================
REM 百联仓库物流管理系统 - 正式环境启动脚本 (Windows版本)
REM 版本: 3.9.0
REM 描述: 自动检查并启动所有必要的服务，然后启动百联系统
REM =============================================================================

echo ==================================
echo 百联仓库物流管理系统 - 正式环境启动脚本
echo 版本: 3.9.0
echo ==================================

REM 检查Java环境
echo [INFO] 检查Java环境...
java -version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] 未检测到Java环境，请先安装Java 8+
    pause
    exit /b 1
)
for /f "tokens=3" %%i in ('java -version 2^>^&1 ^| findstr "version"') do set JAVA_VERSION=%%i
echo [SUCCESS] 检测到Java版本: %JAVA_VERSION%

REM 检查Maven环境
echo [INFO] 检查Maven环境...
mvn -version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] 未检测到Maven环境，请先安装Maven
    pause
    exit /b 1
)
echo [SUCCESS] Maven环境检查通过

REM 检查MySQL服务
echo [INFO] 检查MySQL服务...
sc query mysql >nul 2>&1
if %errorlevel% equ 0 (
    sc query mysql | findstr "RUNNING" >nul
    if %errorlevel% neq 0 (
        echo [WARNING] MySQL服务未运行，尝试启动...
        net start mysql
        timeout /t 3 /nobreak >nul
    )
    sc query mysql | findstr "RUNNING" >nul
    if %errorlevel% equ 0 (
        echo [SUCCESS] MySQL服务运行中
    ) else (
        echo [WARNING] MySQL服务启动失败，请手动启动
    )
) else (
    echo [WARNING] 未检测到MySQL服务，请确保MySQL已安装并运行
)

REM 检查Redis服务
echo [INFO] 检查Redis服务...
sc query redis >nul 2>&1
if %errorlevel% equ 0 (
    sc query redis | findstr "RUNNING" >nul
    if %errorlevel% neq 0 (
        echo [WARNING] Redis服务未运行，尝试启动...
        net start redis
        timeout /t 2 /nobreak >nul
    )
    sc query redis | findstr "RUNNING" >nul
    if %errorlevel% equ 0 (
        echo [SUCCESS] Redis服务运行中
    ) else (
        echo [WARNING] Redis服务启动失败，请手动启动
    )
) else (
    echo [WARNING] 未检测到Redis服务，跳过Redis启动
)

REM 检查Nginx服务
echo [INFO] 检查Nginx服务...
sc query nginx >nul 2>&1
if %errorlevel% equ 0 (
    sc query nginx | findstr "RUNNING" >nul
    if %errorlevel% neq 0 (
        echo [WARNING] Nginx服务未运行，尝试启动...
        net start nginx
        timeout /t 2 /nobreak >nul
    )
    sc query nginx | findstr "RUNNING" >nul
    if %errorlevel% equ 0 (
        echo [SUCCESS] Nginx服务运行中
    ) else (
        echo [WARNING] Nginx服务启动失败，请手动启动
    )
) else (
    echo [WARNING] 未检测到Nginx服务，跳过Nginx启动
)

REM 创建必要目录
echo [INFO] 创建必要目录...
if not exist "logs" mkdir logs
if not exist "uploadPath" mkdir uploadPath
if not exist "temp" mkdir temp
if not exist "backup" mkdir backup

REM 编译项目
echo [INFO] 开始编译项目...
mvn clean package -DskipTests -q
if %errorlevel% neq 0 (
    echo [ERROR] 项目编译失败
    pause
    exit /b 1
)
echo [SUCCESS] 项目编译成功

REM 启动百联系统
echo [INFO] 启动百联系统...
set JAR_FILE=bailian-admin\target\bailian-admin.jar
if not exist "%JAR_FILE%" (
    echo [ERROR] 未找到JAR文件: %JAR_FILE%
    pause
    exit /b 1
)

REM 设置生产环境配置
set SPRING_PROFILES_ACTIVE=prod

REM 启动应用
echo [INFO] 使用生产环境配置启动应用...
start /b java -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -Dspring.profiles.active=prod -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai -jar "%JAR_FILE%" > logs\bailian.log 2>&1

echo [SUCCESS] 百联系统启动中...
echo [INFO] 等待应用启动...

REM 等待应用启动
for /l %%i in (1,1,30) do (
    timeout /t 2 /nobreak >nul
    powershell -Command "try { Invoke-WebRequest -Uri 'http://localhost:8080/actuator/health' -UseBasicParsing | Out-Null; exit 0 } catch { exit 1 }" >nul 2>&1
    if !errorlevel! equ 0 (
        echo.
        echo [SUCCESS] 百联系统启动成功！
        echo [INFO] 访问地址: http://localhost:8080
        echo [INFO] 日志文件: logs\bailian.log
        goto :success
    )
    echo -n .
)

echo.
echo [WARNING] 百联系统启动超时，请检查日志文件: logs\bailian.log

:success
echo ==================================
echo [SUCCESS] 启动脚本执行完成！
echo ==================================
pause 